package com.zyw.utils;

import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternUtils;

import java.io.IOException;

/**
 * 资源加载工具类，用于加载项目中得 文件
 *
 * @author zhangyw
 * @date 2017/12/15 16:45
 */
public class ResourceUtil extends ResourcePatternUtils {

    private static ResourcePatternResolver resourcePatternResolver;

    private static ResourcePatternResolver getResourcePatternResolver() {
        if (resourcePatternResolver == null){
            resourcePatternResolver = new PathMatchingResourcePatternResolver();
        }
        return resourcePatternResolver;
    }

    public static Resource[] getResources(String locationPattern) throws IOException {
        return getResourcePatternResolver().getResources(locationPattern);
    }

    public static Resource getResource(String location) {
        return getResourcePatternResolver().getResource(location);
    }

    public static ClassLoader getClassLoader() {
        return getResourcePatternResolver().getClassLoader();
    }
}
